<template>
	<view class="resourceList">

		<uni-row style="margin-top: 8rpx;">
			<uni-col :span="12" style="padding-right: 5rpx;">
				<uni-data-select placeholder="所有仓库" v-model="queryParams.warehouseId" :localdata="warehouseList"
					style="background-color: #fff;">
				</uni-data-select>
			</uni-col>
			<uni-col :span="12">
				<uni-easyinput style="background-color: #fff;" suffixIcon="search" v-model="queryParams.params.materialName"
					placeholder="查询物料" @iconClick="queryClick"></uni-easyinput>
			</uni-col>
		</uni-row>
		<uni-row>
			<uni-col :span="22" style="padding-left: 5rpx;padding-top: 1rpx;">
				<button type="primary" size="mini" @click="addButtonClick">新增</button>
			</uni-col>
			<uni-col :span="2" style="padding-top: 7rpx;">
				<uni-icons type="list" size="32"></uni-icons>
			</uni-col>
		</uni-row>

		<view v-for="(item,index) in proPickList" style="margin-top: 10rpx;">
			<uni-row class="resourceList-uni-row">
				<text style="font-size: 18px;font-weight:bold;">
					领料单:&nbsp;&nbsp;&nbsp;{{item.proPick.number}}
				</text>
			</uni-row>
			<uni-row class="resourceList-uni-row">
				<text class="resourceList-text">
					领料时间:&nbsp;&nbsp;&nbsp;{{item.proPick.pickTime}}
				</text>
			</uni-row>
			<uni-row class="resourceList-uni-row">
				<text class="resourceList-text">
					领取物料:&nbsp;&nbsp;&nbsp;{{item.materialName}}
				</text>
			</uni-row>
			<uni-row class="resourceList-uni-row">
				<text class="resourceList-text">
					型号:&nbsp;&nbsp;&nbsp;{{item.specification}}
				</text>
			</uni-row>
			<uni-row class="resourceList-uni-row">
				<text class="resourceList-text">
					领取数量:&nbsp;&nbsp;&nbsp;{{item.quantity}} &nbsp;&nbsp;&nbsp;({{item.materialUnitName}})
				</text>
			</uni-row>
			<uni-row class="resourceList-uni-row">
				<text class="resourceList-text">
					领料仓库:&nbsp;&nbsp;&nbsp;{{item.proPick.warehouseName}}
				</text>
			</uni-row>
			<uni-row class="resourceList-uni-row">
				<text class="resourceList-text">
					领料人:&nbsp;&nbsp;&nbsp;{{item.proPick.pickUserName}}
				</text>
			</uni-row>
			<uni-row class="resourceList-uni-row" style="margin-bottom: 20rpx;">
				<text class="resourceList-text">
					备注:&nbsp;&nbsp;&nbsp;{{item.remark}}
				</text>
			</uni-row>
		</view>
		<uni-load-more :status="loadStatus"  />
		<proPickForm ref="proPickForm"  > </proPickForm>
	</view>

</template>

<script>
	import {
		listWarehouse
	} from "@/api/warehouse/warehouse";
	
	import reportQualityForm from "@/pages/quality/reportQuality/form.vue";
	import {
		listProPick,
		delProPick,
		addProPick,
		updateProPick
	} from "@/api/warehouse/proPick"
	import proPickForm  from "@/pages/warehouse/proPick/form.vue";
	import {
		getUserProfile
	} from "@/api/system/user"
	import {
		getDicts
	} from "@/api/system/dict/data";
	import {  listForMobile,getInfoByEntryId,getWorkOrder} from "@/api/production/workOrder";
	import { getMaterial } from "@/api/masterdata/material";
	import { getUnit } from '@/api/masterdata/unit'
	import {
		listInventory,
		getInventory
	} from "@/api/warehouse/inventory";
	export default {
		components: {
			proPickForm
		},
		onLoad: function (option) {
			if(typeof(option.workOrderEntryId)!='undefined'){
				this.queryParams.workOrderEntryId = option.workOrderEntryId;
			}
		},
		data() {
			return {
				queryParams: {
					pageSize: 5,
					pageNum: 1,
					params: {},
					workOrderEntryId:""
				},
				//生产领料单列表
				proPickList: [],
				user: {},
				warehouseList: [],
				loadStatus: "more"
			}
		},
		created() {
			this.getUser();
			this.getList();
			this.getWarehouse();
		},
		 onReachBottom() {
			 this.clickLoadMore()
		 },
		methods: {
			/** 查询列表 */
			getList() {
				this.queryParams.pickUserId=this.user.userId
				listProPick(this.queryParams).then(response => {
					let total =response.total;
					let list = response.rows;
					//已查询出来的和新查询的 大于等于total总页数，表示no-more
					if (list.length+this.proPickList.length >=total) {
						this.loadStatus = "no-more"
					} else {
						this.loadStatus = "more"
					}
					
					if (this.proPickList.length > 0) {
						this.proPickList = this.proPickList.concat(list)
					} else {
						this.proPickList = list;
					}
					
				})
			},
			getUser() {
				getUserProfile().then(response => {
					this.user = response.data
				})
			},
			//点击新增质检
			addReportQuality(id) {
				this.$refs.reportQualityForm.qualityInfo(id);
			},
			addButtonClick(){
				let id =this.queryParams.workOrderEntryId;
				if(id!=''){
					this.$refs.proPickForm.form.workOrderEntryId=id;
					getInfoByEntryId(id).then( res =>{
						let entry = res.data.workOrderEntryList[0];
						this.$refs.proPickForm.isShowFlag=false;
						let _this=this
						getMaterial(entry.pickMaterialId).then(resB =>{
							getUnit(resB.data.unitId).then(resC =>{
								_this.$refs.proPickForm.form.proPickEntryList=[
									{	
										materialId: entry.pickMaterialId,
										materialName: entry.pickMaterialName,
										specification: entry.pickSpecification,
										quantity: entry.pickQuantity,
										materialNumber:resB.data.number,
										materialUnitId:resB.data.unitId,
										materialUnitName:resC.data.name
									}
								]
							})
						})
						//根据该物料查询有库存的仓库
						listInventory({params: {materialIds: entry.pickMaterialId },specification: entry.pickSpecification}).then(reportResponse => {
							let jsonData = reportResponse.rows;
							
							let uniqueWarehouses = jsonData.reduce((accumulator, currentValue) => {
							  // 检查accumulator中是否已经包含了相同warehouseId的对象
							  let existingWarehouse = accumulator.find(
							    warehouse => warehouse.value === currentValue.warehouseId
							  );
							  // 如果不存在相同warehouseId的对象，则将当前对象加入accumulator
							  if (!existingWarehouse) {
							    accumulator.push({ value: currentValue.warehouseId, text: currentValue.warehouseName, id: currentValue.warehouseId, name: currentValue.warehouseName});
							  }
							  return accumulator;
							}, []);
							
							_this.$refs.proPickForm.warehouseList=uniqueWarehouses;
						})
						
						
					})
					
				}
				
				this.$refs.proPickForm.openPopup();
			},
			queryClick(){
				this.queryParams.pageNum = 1
				this.proPickList = []
				this.getList();
			},
			getWarehouse(){
				listWarehouse({pageSize: 99999}).then(response => {
					let list = [];
					list = response.rows;
					for (let i = 0; i < list.length; i++) {
						list[i].value = list[i].id;
						list[i].text = list[i].name;
						this.warehouseList.push(list[i]);
					}
				});
			},
			clickLoadMore() {
				if (this.loadStatus === "no-more") {
					return;
				}
				this.loadStatus = "loading"
				this.queryParams.pageNum += 1;
				this.getList();
			}
		},
	}
</script>

<style>
	.resourceList-uni-row {
		margin-bottom: 8rpx;
	}

	.resourceList-text {
		font-size: 16px;
	}

	.resourceList {
		margin-left: 10px;

	}
</style>
